package com.ymatou.iapi.godmch.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.AgentInfo;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.applyAgentBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.getAgentRebateRateBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.createagentCall;
import com.ymatou.iapi.godmch.service.createmchCall;
import com.ymatou.iapi.godmch.service.getAgentRebateRateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
import com.ymttest.utils.TestDataManager;
/**
*批量查询代理返佣比例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentRebateRate{
	private static getAgentRebateRateBean bean;
	private static getAgentRebateRateCall call;
	private static CreateCodeBean ccbean = new CreateCodeBean();
	public agent_typeWapper agentTypeWapper = new agent_typeWapper();
	mch_relationWapper mch_relationwapeper=new mch_relationWapper();
	
	private  createmchBean createmchbean=new createmchBean();
	private  createmchCall createmchcall=new createmchCall();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询代理返佣比例");
	}

	@Before
	public void caseUp() {
		bean=new getAgentRebateRateBean();
		call=new getAgentRebateRateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/*
	 * 创建平台代理
	 */
	private void createAgent(Integer agentId,String code) throws ClientProtocolException, IOException
	{
		Logger.comment("###########创建代理###########");
		createmchBean createmchBean_agent = new createmchBean();
		createmchCall createmchCall = new createmchCall();

		createmchBean_agent.setMchId(agentId);
		createmchBean_agent.setCode(code);
		createmchBean_agent.setCountry("USA");
		createmchBean_agent.setNickName("测试");
		createmchCall.setData(createmchBean_agent);
		createmchCall.callService();
	}
	/**
	 * 申请独立商户代理
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * @throws JSONException 
	 * @throws InterruptedException 
	 */
     private applyAgentCall applyAgent(CreateCodeBean createCodeBean,createIndepMchCall createCall) throws ClientProtocolException, IOException, JSONException, InterruptedException
     {
 		CreateCodeCall createCodeCall=GodmchManageCallService.createCodeCall(createCodeBean);
		applyAgentBean applyAgentBean=new applyAgentBean();
		applyAgentCall applyAgentCall=new applyAgentCall();
		Thread.sleep(5000);//休眠目的是为了创建独立商户，把对应的配置  同步到用户那边，然后用户 拿到配置再创建代理
		applyAgentBean.setMchId(createCall.getmchId());
		applyAgentBean.setCountry("CHN");
		applyAgentBean.setMobile("186"+TestDataManager.getRandomIntNum(8));
		applyAgentBean.setPassword("111aaaa");
		applyAgentBean.setAppKey("appsqSeller_"+createCall.getmchId());
		applyAgentBean.setAppSource(60);
		applyAgentBean.setTerminalSource(3);
		applyAgentBean.setInviteCode(createCodeCall.getCode());
		applyAgentCall.setData(applyAgentBean);
		applyAgentCall.callService();	
		return applyAgentCall;
     }
	private void check_verify(Integer mchId,Integer agentId) throws InterruptedException, JSONException
	{
		Logger.verifyEquals("200", call.getStatus1(), "验证返回的status");
		JSONArray obj=call.getRebateRateList();
		List<mch_relation>  relations=mch_relationwapeper.selectByPrimaryKey(agentId, mchId);
		for(mch_relation relation:relations)
		{
			Map<String, Object> map = new HashMap();
			map.put("mchId", mchId);
			map.put("agentType", relation.getAgentType());
			agent_type type = agentTypeWapper.selectby(map).get(0);
			Logger.verifyEquals(new BigDecimal(obj.getJSONObject(0).getDouble("rebateRate")).stripTrailingZeros().toPlainString(),type.getRebateRate().stripTrailingZeros().toPlainString(), "验证返回的佣金");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRebateRate_001() {
		Logger.start(true,"批量查询代理返佣比例-验证代理全是自主选货,无返佣");
		try {
			//新增渠道商
			ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=1;
			for(int i=0;i<count;i++) {
				Integer agentid=GodmchManageCallService.CreateSellerMch(code);
			}
			int mchId = ccbean.getMchId();
			
//			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
//			createagentBean.setAgentMode(1);//1 - 自主选货（分销代理） 2 - 自动继承（分销小店）
//			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			Integer agentId = DGUserCallService.UserCreateCall(usercreateBean);
			//创建代理
			createAgent(agentId,code);
			
			List<AgentInfo> infos=new ArrayList<AgentInfo>();
			AgentInfo info=new AgentInfo();
		    info.setMchId(mchId);
			info.setAgentId(agentId);
			infos.add(info);
			bean.setAgentList(infos);
			call.setData(bean);
			call.callService();		
			
			check_verify( mchId, agentId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRebateRate_002() {
		Logger.start(true,"批量查询代理返佣比例-验证代理包含自主继承,有返佣");
		//自主继承只能是独立商户有，平台目前不能是自主继承
		try {
			
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//邀请码
			CreateCodeBean createCodeBean=GodmchManageCallService.InitcreateCodeBeanForAgent(createCall.getmchId(), true);
			// 创建代理类型
			createagentBean createagentBean = GodmchManageCallService.InitCreateAgentTypeBean(createCall.getmchId());
			createagentBean.setAgentMode(2);//
			createagentBean.setRebateRate(new BigDecimal(20));
			createagentCall createagentcall = GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			//查询类型
			agent_typeWapper agentTypewapper = new agent_typeWapper();
			Map<String, Object> map = new HashMap();
			map.put("mchId", createCall.getmchId());
			List<agent_type> agenttypeList = agentTypewapper.selectby(map);
			createCodeBean.setRoleType(agenttypeList.get(2).getAgentType());
			//申请独立商户
			applyAgentCall applyAgentCall=applyAgent(createCodeBean,createCall);
			
			List<AgentInfo> infos=new ArrayList<AgentInfo>();
			AgentInfo info=new AgentInfo();
		    info.setMchId(createCall.getmchId());
			info.setAgentId(applyAgentCall.getAgentId());
			infos.add(info);
			bean.setAgentList(infos);
			call.setData(bean);
			call.callService();		
			
			check_verify( createCall.getmchId(), applyAgentCall.getAgentId());	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRebateRate_003() {
		Logger.start(true,"批量查询代理返佣比例-验证代理自主继承,返佣为20");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//邀请码
			CreateCodeBean createCodeBean=GodmchManageCallService.InitcreateCodeBeanForAgent(createCall.getmchId(), true);
			// 创建代理类型
			createagentBean createagentBean = GodmchManageCallService.InitCreateAgentTypeBean(createCall.getmchId());
			createagentBean.setAgentMode(2);//
			createagentBean.setRebateRate(new BigDecimal(20));
			createagentCall createagentcall = GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			//查询类型
			agent_typeWapper agentTypewapper = new agent_typeWapper();
			Map<String, Object> map = new HashMap();
			map.put("mchId", createCall.getmchId());
			List<agent_type> agenttypeList = agentTypewapper.selectby(map);
			createCodeBean.setRoleType(agenttypeList.get(2).getAgentType());
			
			//申请独立商户
			applyAgentCall applyAgentCall=applyAgent(createCodeBean,createCall);	
			
			List<AgentInfo> infos=new ArrayList<AgentInfo>();
			AgentInfo info=new AgentInfo();
		    info.setMchId(createCall.getmchId());
			info.setAgentId(applyAgentCall.getAgentId());
			infos.add(info);
			bean.setAgentList(infos);
			call.setData(bean);
			call.callService();		
			
			check_verify( createCall.getmchId(), applyAgentCall.getAgentId());		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRebateRate_005() {
		Logger.start(true,"批量查询代理返佣比例-验证代理商列表为空");
		try {
			
			//List<AgentInfo> infos=new ArrayList<AgentInfo>();
			bean.setAgentList(null);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getStatus1(), "验证返回的status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRebateRate_006() {
		Logger.start(true,"批量查询代理返佣比例-验证agentId为空");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//邀请码
			CreateCodeBean createCodeBean=GodmchManageCallService.InitcreateCodeBeanForAgent(createCall.getmchId(), true);
			// 创建代理类型
			createagentBean createagentBean = GodmchManageCallService.InitCreateAgentTypeBean(createCall.getmchId());
			createagentBean.setAgentMode(2);//
			createagentBean.setRebateRate(new BigDecimal(20));
			createagentCall createagentcall = GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			//查询类型
			agent_typeWapper agentTypewapper = new agent_typeWapper();
			Map<String, Object> map = new HashMap();
			map.put("mchId", createCall.getmchId());
			List<agent_type> agenttypeList = agentTypewapper.selectby(map);
			createCodeBean.setRoleType(agenttypeList.get(2).getAgentType());
			
			//申请独立商户
			//applyAgentCall applyAgentCall=applyAgent(createCodeBean,createCall);	
			
			List<AgentInfo> infos=new ArrayList<AgentInfo>();
			AgentInfo info=new AgentInfo();
		    info.setMchId(createCall.getmchId());
			//info.setAgentId(applyAgentCall.getAgentId());
			infos.add(info);
			bean.setAgentList(infos);
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("4001", call.getStatus1(), "验证返回的status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRebateRate_007() {
		Logger.start(true,"批量查询代理返佣比例-验证mchId为空");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//邀请码
			CreateCodeBean createCodeBean=GodmchManageCallService.InitcreateCodeBeanForAgent(createCall.getmchId(), true);
			// 创建代理类型
			createagentBean createagentBean = GodmchManageCallService.InitCreateAgentTypeBean(createCall.getmchId());
			createagentBean.setAgentMode(2);//
			createagentBean.setRebateRate(new BigDecimal(20));
			createagentCall createagentcall = GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			//查询类型
			agent_typeWapper agentTypewapper = new agent_typeWapper();
			Map<String, Object> map = new HashMap();
			map.put("mchId", createCall.getmchId());
			List<agent_type> agenttypeList = agentTypewapper.selectby(map);
			createCodeBean.setRoleType(agenttypeList.get(2).getAgentType());
			
			//申请独立商户
			applyAgentCall applyAgentCall=applyAgent(createCodeBean,createCall);	
			
			List<AgentInfo> infos=new ArrayList<AgentInfo>();
			AgentInfo info=new AgentInfo();
		   // info.setMchId(createCall.getmchId());
			info.setAgentId(applyAgentCall.getAgentId());
			infos.add(info);
			bean.setAgentList(infos);
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("4001", call.getStatus1(), "验证返回的status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}